#include "DxChart.h"

CDxChart::CDxChart() : pdxiBackground(0) , pdxiMarker(0) {}

CDxChart::~CDxChart() {}

void	CDxChart::Set(CDxImage * _pdxiBG, CDxImage * _pdxiMarker)
{
	pdxiBackground = _pdxiBG;
	pdxiMarker = _pdxiMarker;
}

void	CDxChart::AddAnchor(int _nId, float _fX, float _fY)
{
	tAnchor	anchor;

	// Set the anchor's contents
	anchor.nId	= _nId;
	anchor.fX	= _fX;
	anchor.fY	= _fY;

	// push it onto our vector
	vecAnchors.push_back(anchor);
}

void	CDxChart::SetAnchor(int _nId)
{
	tAnchor * pAnchorToSet = NULL;

	// Search for an anchor with the specified ID
	for(unsigned int i = 0; i < vecAnchors.size(); i++)
	{
		// Check the id
		if(vecAnchors[i].nId == _nId)
			pAnchorToSet = &vecAnchors[i];	// Set our pointer when found
	}

	// If the anchor was found, unset all anchors THEN set the one we want.
	if(pAnchorToSet)
	{
		for(unsigned int i = 0; i < vecAnchors.size(); i++)
		{
			// unset
			vecAnchors[i].bSet = false;
		}

		// Set our anchor
		pAnchorToSet->bSet = true;
	}
}

void	CDxChart::Draw(D3DXMATRIX	*	_pMatrix, bool _bSpriteDrawInProgress)
{
	// Check if vitals are set
	if(!pdxiBackground || !pdxiMarker)
		return;

	tAnchor * pAnchorToDraw = NULL;

	// Search for the currently set anchor
	for(unsigned int i = 0; i < vecAnchors.size(); i++)
	{
		// Check the id
		if(vecAnchors[i].bSet)
			pAnchorToDraw = &vecAnchors[i];	// Set our pointer when found
	}

	// Begin sprite drawing
	if(!_bSpriteDrawInProgress)
		d3dManager.BeginSprites();
	
	// Draw animation frame
	d3dManager.Sprite( pdxiBackground->GetTexture(), _pMatrix);

	// update the matrix for the set anchor
	if(pAnchorToDraw)
	{
		D3DXMATRIX anchorMatrix(*_pMatrix);
		anchorMatrix._41 += pAnchorToDraw->fX - (pdxiMarker->GetWidth() * 0.5f);
		anchorMatrix._42 += pAnchorToDraw->fY - (pdxiMarker->GetHeight() * 0.5f);

		// Draw a marker on the anchor
		d3dManager.Sprite( pdxiMarker->GetTexture(), &anchorMatrix );
	}

	// End sprite drawing
	if(!_bSpriteDrawInProgress)
		d3dManager.EndSprites();
}
